package com.itheima.controller;

import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RequestMapping("/clazzs")
@RestController
public class ClazzController {
    @Autowired
    private ClazzService clazzService;
    @GetMapping("/list")
    public Result list(String name, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate end, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize){
        PageResult<Clazz> list = clazzService.list(name, begin, end, page, pageSize);
        return Result.success(list);
    }
    @GetMapping("/{id}")
    public Result queryById(@PathVariable("id") Integer id){
        log.info("根基id查询班级信息:{}", id);
        Clazz clazz = clazzService.queryById(id);
        return Result.success(clazz);
    }
    @PostMapping
    public Result addClazz(@RequestBody Clazz clazz){
        log.info("增加班级:{}",clazz);
        clazzService.addClazz(clazz);
        return Result.success();
    }

    @PutMapping
    public Result updateClazz(@RequestBody Clazz clazz){
        log.info("修改班级:{}",clazz);
        clazzService.updateClazz(clazz);
        return Result.success();
    }
    @DeleteMapping("/{id}")
    public Result deleteClazz(@PathVariable("id") Integer id){
        log.info("删除班级:{}",id);
        clazzService.deleteClazz(id);
        return Result.success();
    }

}
